package world;

import hubol.Sprite;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;

import com.jogamp.opengl.GL;

import owg.util.ClassPathStream;
import owg.util.NamedInputStream;
import owg.util.ProjectRelativeStream;
import owg.util.opengl.Tex;
import owg.util.opengl.TextureLib;
import bogus.core.BogusCore;
import bogus.main.BogusMkII;
import bogus.minigame.MinigameBase;
import bogus.settings.Settings;
import audio.Audio;

public class Global {
    private static ArrayList<Tex> contentSprite = new ArrayList<Tex>();
    private static ArrayList<String> contentAudio = new ArrayList<String>();

    public static File contentDirectory;

    public final static File SETTINGS = fileRelative("settings/settings.txt");
    public final static File SAVEFILE = fileRelative("res/save.bogus");
    public final static File MUSIC_DIRECTORY = fileRelative("res/music");

    public static MinigameBase minigame;
    public static World world;

    public static Sprite quit;

    /**change the minigame reference, destroying the old Minigame*/
    public static void setMinigame(MinigameBase m){
	if (minigame != m && minigame != null)
	    minigame.dispose();
	minigame = m;
    }

    /**this will unload sprites and audio loaded for a minigame*/
    public static void contentUnload(){
	GL gl = BogusCore.egl.getGL();
	for (Tex s : contentSprite)
	    s.destroy(gl);
	for (String s : contentAudio){
	    if (Audio.exists(s))
		Audio.delete(s);
	}
    }

    /**this function calls contentUnload(), then loads all sprites and audio found in the folder appropriate to the minigame, stores references to the
     * loaded materials so they can be unloaded at a later time*/
    public static void contentLoad(String id){
	contentUnload();

	contentDirectory = fileRelative("res/content/"+id);

	contentSprite = TextureLib.loadDir(contentRelative("sprites").getAbsolutePath(), Settings.settings.forcePOTS);
	for (String s : Audio.loadDir(contentRelative("sounds").getPath()))
	    contentAudio.add(s);
    }

    /**this function returns a subfolder/file contained within the main directory*/
    public static File fileRelative(String f){
	return new File(BogusMkII.DIRECTORY, f);
    }
    /**this function returns a subfolder/file contained within the main directory
     * @throws FileNotFoundException If the file does not exist*/
    public static NamedInputStream streamRelative(String f) throws FileNotFoundException{
	return new ProjectRelativeStream(new File(BogusMkII.DIRECTORY), f);
    }
    /**returns an input stream for a file relative to the classpath, typically the root of the jar file.
     * @throws FileNotFoundException If the file does not exist*/
    public static NamedInputStream jarFile(String f) throws FileNotFoundException{
	return new ClassPathStream(Global.class, f);
    }
    /**this function returns a subfolder/file contained within the minigame content directory*/
    public static File contentRelative(String f){
	return new File(contentDirectory.getPath(), f);
    }

}
